package com.duqc.qiangdan;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.params.SetParams;

/**
 * @ClassName RedisUtil
 * @Description RedisUtil
 * @Author duqc
 * @Date 2022/1/28
 **/
@Slf4j
@Configuration
@RequiredArgsConstructor
public class RedisConfigUtil extends CachingConfigurerSupport {

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private int port;

    @Value("${spring.redis.timeout}")
    private int timeout;

    @Value("${spring.redis.password}")
    private String password;

    @Value("${spring.redis.database}")
    private int database;

    @Value("${spring.redis.jedis.pool.max-idle}")
    private int maxIdle;

    @Value("${spring.redis.jedis.pool.max-wait}")
    private long maxWaitMillis;

    @Bean
    public JedisPool redisPoolFactory() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(maxIdle);
        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
        JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password, database);
        return jedisPool;
    }

    /**
     * 加锁
     *
     * @param key
     * @param val
     * @return Boolean
     */
    public boolean setnx(String key, String val) {
        Jedis jedis = null;
        try {
            jedis = redisPoolFactory().getResource();
            if (null == jedis) {
                return false;
            }
            SetParams setParams = SetParams.setParams();
            setParams.nx();
            setParams.px(1000 * 60);
            String set = jedis.set(key, val, setParams);
            log.info("set--------------------------" + set);
            if (StringUtils.isBlank(set)) {
                return false;
            }
            return set.equalsIgnoreCase("ok");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            if (null != jedis) {
                jedis.close();
            }
        }
        return false;
    }

    /**
     * 解锁
     *
     * @param key
     * @param val
     * @return int
     */
    public int delnx(String key, String val) {
        Jedis jedis = null;
        try {
            jedis = redisPoolFactory().getResource();
            if (null == jedis) {
                return 0;
            }
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("if redis.call('get','").append(key).append("')").append("=='").append(val).append("'").
                    append(" then ").
                    append("    return redis.call('del','").append(key).append("')").
                    append(" else ").
                    append("    return 0").
                    append(" end");
            String value = String.valueOf(jedis.eval(stringBuilder.toString()));
            log.info("value-------------------------" + value);
            return Integer.valueOf(value);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            if (null != jedis) {
                jedis.close();
            }
        }
        return 0;
    }

}
